#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtDebug>
#include <QMenuBar>
#include <QNetworkProxy>

#include "request_handler_base.h"

#include "home_view.h"
#include "activity_category_view.h"
#include "place_category_view.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    manager_(0),
    homeView_(0),
    activityCategoryView_(0),
    placeCategoryView_(0),
    menu_(0)
{
  ui->setupUi(this);
  SetupCBA();

  manager_ = new QNetworkAccessManager(this);

  requestHandler_ = new RequestHandler(manager_);
  SetState(ActivityCategory);

//  showHomeView();
}


void MainWindow::SetupCBA() {

#if defined(Q_WS_S60)
  // Create menu
  menu_ = new QMenu(this);
  menu_->addAction("Home", this, SLOT(showHomeView()));
  menu_->addAction("Place", this, SLOT(showPlaceView()));
  menu_->addAction("Event", this, SLOT(showActivityView()));
  menu_->addAction("Nearby", this, SLOT(showNearbyView()));

  // Create Options CBA
  QAction *optionsAction = new QAction("Go to", this);
  // Set defined menu into Options button
  optionsAction->setMenu(menu_);
  optionsAction->setSoftKeyRole(QAction::PositiveSoftKey);
  addAction(optionsAction);

  QAction *backSoftkeyAction = new QAction(QString("Exit"), this);
  backSoftkeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
  QObject::connect(backSoftkeyAction, SIGNAL(triggered()),
                   this, SLOT(close()));
  addAction(backSoftkeyAction);

#else
  menuBar()->addAction("Home", this, SLOT(showHomeView()));
  menuBar()->addAction("Place", this, SLOT(showPlaceView()));
  menuBar()->addAction("Event", this, SLOT(showActivityView()));
  menuBar()->addAction("Nearby", this, SLOT(showNearbyView()));
#endif
}

MainWindow::~MainWindow()
{
  if (requestHandler_) {
    delete requestHandler_;
  }
  if (manager_) {
    manager_->disconnect();
    delete manager_;
  }
  if (menu_) {
    delete menu_;
  }
  if (homeView_) {
    delete homeView_;
  }
  if (activityCategoryView_) {
    delete activityCategoryView_;
  }
  if (placeCategoryView_) {
      delete placeCategoryView_;
  }
  if (ui) {
    delete ui;
  }
}

void MainWindow::RewindUI() {
  qDebug() << "Back pressed";
}

void MainWindow::onItemClicked(QListWidgetItem *item) {
  qDebug() << item->text();
}

void MainWindow::showHomeView() {
  if (!homeView_) {
    homeView_ = new HomeView(this);
    homeView_->SetRequestHandler(requestHandler_);
  }

  homeView_->Show();
  homeView_->IssueRequest();
}

void MainWindow::showPlaceView() {
    if (!placeCategoryView_) {
        placeCategoryView_ = new PlaceCategoryView(this);
        placeCategoryView_->SetRequestHandler(requestHandler_);
    }

    placeCategoryView_->Show();
    placeCategoryView_->IssueRequest();
}

void MainWindow::showActivityView() {
  if (!activityCategoryView_) {
    activityCategoryView_ = new ActivityCategoryView(this);
    activityCategoryView_->SetRequestHandler(requestHandler_);
  }

  activityCategoryView_->Show();
  activityCategoryView_->IssueRequest();
}

void MainWindow::showNearbyView() {

}
